package tt.global;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import tt.fileutils.FileUtils;
import tt.treader.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

public class ExplorerAdapter extends BaseAdapter {
	private LayoutInflater mInflater;
	private Bitmap mIcon_folder;
	private Bitmap mIcon_file;
	private Bitmap mIcon_text;
	private List<String> items;
	private List<String> paths;
	private List<String> sizes;
	private List<Boolean> checkBoxesStatus;
	private HashMap<String, HashMap<String, String>> checkedList = new HashMap<String, HashMap<String, String>>();
	private int itemsSize = 0;

	public ExplorerAdapter(Context context, List<String> items,
			List<String> paths, List<String> sizes,
			HashMap<String, HashMap<String, String>> checkedList) {
		mInflater = LayoutInflater.from(context);
		this.items = items;
		this.paths = paths;
		this.sizes = sizes;
		itemsSize = items.size();
		this.checkedList = checkedList;
		checkBoxesStatus = new ArrayList<Boolean>(itemsSize);
		for (int i = 0; i < itemsSize; i++) {
			checkBoxesStatus.add(false);
		}
		mIcon_folder = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.folder);
		mIcon_file = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.file);
		mIcon_text = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.text);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		Boolean checkBoxStatus = checkBoxesStatus.get(position);
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.explorer_items, null);
			holder = new ViewHolder();
			holder.f_title = (TextView) convertView.findViewById(R.id.f_title);
			holder.f_size = (TextView) convertView.findViewById(R.id.f_size);
			holder.f_icon = (ImageView) convertView.findViewById(R.id.f_icon);
			holder.f_cheCheckBox = (CheckBox) convertView
					.findViewById(R.id.f_checkbox);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		File file = new File(paths.get(position).toString());
		holder.f_title.setText(file.getName());
		String f_type = new FileUtils().getFileType(file);

		if (file.isDirectory()) {
			holder.f_icon.setImageBitmap(mIcon_folder);
			holder.f_size.setText("");
			holder.f_cheCheckBox.setVisibility(View.GONE);
		} else {
			holder.f_size.setText(sizes.get(position));
			if (f_type.equals("txt")) {
				holder.f_icon.setImageBitmap(mIcon_text);

				holder.f_cheCheckBox.setVisibility(View.VISIBLE);

				holder.f_cheCheckBox.setId(position);
				holder.f_cheCheckBox.setChecked(checkBoxStatus);
				holder.f_cheCheckBox
						.setOnCheckedChangeListener(new OnCheckedChangeListener() {
							@Override
							public void onCheckedChanged(
									CompoundButton buttonView, boolean isChecked) {
								if (isChecked) {
									final HashMap<String, String> checkedItem = new HashMap<String, String>();
									checkBoxesStatus.set(buttonView.getId(),
											isChecked);
									checkedItem.put(
											GlobalConstants.IMPORTED_BOOK_NAME,
											items.get(buttonView.getId())
													.toString());
									checkedItem.put(
											GlobalConstants.IMPORTED_BOOK_PATH,
											paths.get(buttonView.getId())
													.toString());
									checkedList.put(
											String.valueOf(buttonView.getId()),
											checkedItem);
								} else {
									checkBoxesStatus.set(buttonView.getId(),
											isChecked);
									checkedList.remove(String
											.valueOf(buttonView.getId()));
								}
								notifyDataSetChanged();
							}
						});
			} else {
				holder.f_icon.setImageBitmap(mIcon_file);
				holder.f_cheCheckBox.setVisibility(View.GONE);
			}
		}
		return convertView;
	}

	private class ViewHolder {
		TextView f_title;
		TextView f_size;
		ImageView f_icon;
		CheckBox f_cheCheckBox;
	}
}
